package Card.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;

import Card.connect.CardClient;

/*
 * The login window.
 * @author Juthamas Uthamaphethai 5510546964
 * @author Natcha Chidchob 5510546026
 */
public class LoginUI extends JFrame implements Observer {
	/**textfiled to get the name of player.*/
	private JTextField username;
	/**Client who join the game.*/
	private CardClient client;
	private JButton start;
	private JLabel status;
	private JPanel bigPane;
	/**list of login person.*/
	private List list;
	private JButton login;
	/**number of person who login.*/
	private int check = 0;
	/**check if the start button can press or not.*/
	private boolean isMore = false;

	/*
	 * Constructor for login UI.
	 */
	public LoginUI(CardClient client) {
		super("LOG IN");
		this.client = client;
		initComponents();
	}

	/*
	 *Make up all components in login window.
	 */
	private void initComponents() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					client.closeConnection();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		bigPane = new JPanel();
		bigPane.setLayout(new BoxLayout(bigPane, MAXIMIZED_HORIZ));

		list = new List();
		list.setSize(200, 300);

		ClassLoader cldr = this.getClass().getClassLoader();
		java.net.URL imageURL = cldr.getResource("bg/loginbg2.jpg");
		ImageIcon pic = new ImageIcon(imageURL);
		ImagePanel LogInpane = new ImagePanel(pic.getImage());

		username = new JTextField();
		username.setColumns(20);

		start = new JButton("START");
		start.setBackground(UIManager.getColor("Button.background"));
		start.setFont(new Font("Tahoma", Font.BOLD, 13));
		start.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					client.sendToServer(username.getText());
					client.sendToServer("open game");
					setVisible(false);
					client.sendToServer("send card");
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
		});

		JLabel lblPleaseEnterYour = new JLabel("Please enter your name");
		lblPleaseEnterYour.setForeground(new Color(0, 0, 153));
		lblPleaseEnterYour.setFont(new Font("Monospaced", Font.BOLD, 22));

		status = new JLabel("Note : Waiting for another player.");
		status.setForeground(new Color(128, 0, 0));
		status.setFont(new Font("Tahoma", Font.BOLD, 11));

		login = new JButton("Login");
		login.setFont(new Font("Tahoma", Font.BOLD, 12));
		login.setEnabled(true);
		if(client.getID()==2)
		login.setEnabled(false);
		GroupLayout gl_pane = new GroupLayout(LogInpane);
		gl_pane.setHorizontalGroup(
			gl_pane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pane.createSequentialGroup()
					.addGroup(gl_pane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_pane.createSequentialGroup()
							.addGap(100)
							.addComponent(username, GroupLayout.PREFERRED_SIZE, 215, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_pane.createSequentialGroup()
							.addGap(68)
							.addComponent(lblPleaseEnterYour))
						.addGroup(gl_pane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_pane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_pane.createSequentialGroup()
									.addComponent(login)
									.addGap(20)
									.addComponent(start, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_pane.createSequentialGroup()
									.addComponent(status)
									.addGap(94)))))
					.addContainerGap(46, Short.MAX_VALUE))
		);
		gl_pane.setVerticalGroup(
			gl_pane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pane.createSequentialGroup()
					.addGap(37)
					.addComponent(lblPleaseEnterYour)
					.addGap(31)
					.addComponent(username, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
					.addGap(38)
					.addGroup(gl_pane.createParallelGroup(Alignment.BASELINE)
						.addComponent(login)
						.addComponent(start, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
					.addComponent(status)
					.addContainerGap())
		);
		start.setEnabled(false);
		username.addActionListener(messageActionListener);
		login.addActionListener(messageActionListener);
		LogInpane.setLayout(gl_pane);
		bigPane.add(LogInpane);
		bigPane.add(list);
		getContentPane().add(bigPane);
	}

	/*
	 * Run up the application.
	 */
	public void run() {
		pack();
		setSize(550, 300);
		setResizable(false);
		setVisible(true);
	}

	ActionListener messageActionListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			try {
				client.sendToServer(username.getText());
				username.setText("");
				try {
					client.sendToServer("login");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				login.setEnabled(false);

			} catch (Exception ex) {
				list.add(ex.toString());
				list.makeVisible(list.getItemCount() - 1);
				list.setBackground(Color.yellow);
			}
		}
	};

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof String) {
			String word = (String) arg;
			if (word.equals("start") && check == 0) {
				CardUI ui = new CardUI(client);
				client.addObserver(ui);
				ui.run();
				dispose();
				try {
					client.sendToServer("give name");
				} catch (IOException e) {
					e.printStackTrace();
				}
				setVisible(false);
				check = 1;
			}
			if (word.equals("can start")) {
				System.out.println(isMore);
				if (isMore) {
					start.setEnabled(true);
					isMore = false;
				}
			}
			if(word.equals("next login")){
				if(client.getID()==2)
				login.setEnabled(true);
			}
		}
		if (arg instanceof String[]) {
			String[] a = (String[]) arg;
			list.add(a[a.length - 1] + " has login!");
		}
		if (arg instanceof Byte) {
			Byte count = (Byte) arg;
			if (count == 2) {
				if (client.isCheckStart() == true)
					isMore = true;
			} else if (count > 2) {
				start.setEnabled(false);
				status.setText("too much people.");
			}
		}

	}
}

